import App from './App'
import uView from './uview-ui';

import propsConfig from './components/thorui/tui-config/index.js'
//全局组件配置
uni.$tui = propsConfig

uni.$u.setConfig({
	config: {
		unit: 'rpx'
	}
})

uni.$u.http.setConfig((config) => {
	/* config 为默认全局配置*/
	 // config.baseURL = `http://101.37.21.34:8890/app`; /* 根域名 */
	// config.baseURL = `https://api.zhaodazi.club/dev-api/app`;
	config.baseURL = `https://api.wddzq.com/prod-api/app`;
	return config
})

let timer = null;
uni.$u.http.interceptors.response.use((response) => {
	console.log(response);
	let { success, code } = response.data || {};
	if(code === 401) {
		uni.setStorageSync('token', '');
		uni.setStorageSync('userInfo', '');
		if(timer) clearTimeout(timer);
		timer = setTimeout(() => {
			uni.navigateTo({
				url: '/pages/login/login'
			})
		}, 500);

		return response?.data;
	}
	if(success) {
		return response?.data
	} else {
		return Promise.reject(response?.data);
	}
}, (response) => {
	/*  对响应错误做点什么 （statusCode !== 200）*/
	return Promise.reject(response?.data);
})
setHttpHeader()
export function setHttpHeader(){
	var token = uni.getStorageSync('token');
	var LNGLAT = uni.getStorageSync('LNGLAT');
	uni.$u.http.setConfig((config) => {
		/* config 为默认全局配置*/
		config.header = {
			Authorization: token,
			LNGLAT: LNGLAT,
		};
		return config
	})
}

// 设置token
// uni.getStorage({
// 	key: 'token',
// 	success(res) {
// 		uni.$u.http.setConfig((config) => {
// 			/* config 为默认全局配置*/
// 			config.header = {
// 				Authorization: res.data
// 			};
// 			return config
// 		})
// 	}
// })

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false

Vue.use(uView);

App.mpType = 'app'
const app = new Vue({
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'

export function createApp() {
	console.log('>>>>>>>>>>>>>>>>start >>>>>>>>>>>>>>>>>>>>');
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif
